package com.aliyun.messagemgt.web.controller;

import com.aliyun.messagemgt.application.service.ChannelNoServer;
import com.aliyun.messagemgt.common.annotation.OperateLogAnnotation;
import com.aliyun.messagemgt.common.constants.MgtCommonConst;
import com.aliyun.messagemgt.common.enums.OperateTypeEnum;
import com.aliyun.messagemgt.common.enums.ResourceTypeEnum;
import com.aliyun.messagemgt.common.errorcode.CommonErrorCode;
import com.aliyun.messagemgt.common.exceptions.BaseException;
import com.aliyun.messagemgt.dto.ChannelNo;
import com.aliyun.messagemgt.dto.query.ChannelNoQuery;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 *@Author Znight
 *@Date 15:00 2019/6/24
 * 渠道号管理
 */
@Api(tags = "渠道号管理相关接口")
@CrossOrigin
@RestController
@RequestMapping(value = "/channelNo")
public class ChannelNoController {

    Logger logger = LoggerFactory.getLogger(ChannelNoController.class);
    @Autowired
    private ChannelNoServer channelNoServer;

    @RequestMapping("/del")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.PUSHCHANNEL ,operateType = OperateTypeEnum.DEL,
            resouceClass = ChannelNoQuery.class,resouceIdField = "channelUserId")
    public Object delChannelNo(@RequestBody ChannelNoQuery channelNoQuery){
        return channelNoServer.delChannelNo(channelNoQuery);
    }

    @RequestMapping("/hidden")
    public Object hiddenChannelNo(@RequestBody ChannelNoQuery channelNoQuery){
        return channelNoServer.hiddenChannelNo(channelNoQuery);
    }

    @RequestMapping("/mod")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.PUSHCHANNEL ,operateType = OperateTypeEnum.MOD,
            resouceClass = ChannelNo.class,resouceIdField = "channelUserId")
    public Object modChannelNo(@RequestBody ChannelNo channelNo){
        return channelNoServer.modChannelNo(channelNo);
    }

    @RequestMapping("/detail")
    public Object detailChannelNo(@RequestBody ChannelNoQuery channelNo){
        return channelNoServer.detailChannelNo(channelNo);
    }

    @RequestMapping("/inExcel")
    public Object inExcel(@RequestParam MultipartFile file) {
        String name = file.getOriginalFilename();
        if (name.endsWith(MgtCommonConst.FILE_EXCEL_XLS)||name.endsWith(MgtCommonConst.FILE_EXCEL_XLSX)){
            try {
                return channelNoServer.inExcel(file);
            } catch (DataAccessException e) {
               logger.error("inExcel DataAccessException: ",e);
                throw new BaseException(CommonErrorCode.FILE_FAIL);
            } catch (IOException e) {
                logger.error("inExcel IOException: ",e);
                throw new BaseException(CommonErrorCode.EXCEL_UPDATE);
            }
        }else{
            throw new BaseException(CommonErrorCode.FILE_TYPE_FAIL);
        }

    }

}
